﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyDay13.Context;
using MyDay13.IRepostitype;
using MyDay13.IService;
using MyDay13.JWTModels;
using MyDay13.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyDay13.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class JWTSController : ControllerBase
    {

            private readonly JWTTokenOptions _tokenOptions;
        IUserRepostitype user;

            public JWTSController(JWTTokenOptions tokenOptions, IUserRepostitype userService)
            {
                _tokenOptions = tokenOptions;
                user = userService;
            }
            [HttpGet]
            [Route("MyLogin")]
            [AllowAnonymous]
            public string Login(string name, string pwd)
            {
               var flag= user.Login(name, pwd);
                //返回的结果
                bool result = false;
                //返回的token的结果集默认为null
                TokenResult JwtToken = new TokenResult();
                if (flag != 0)
                {
                    //生成teken
                    JwtToken = new JwtTokenHelper().AuthorizeToken(name, _tokenOptions);
                    return JsonConvert.SerializeObject(new
                    {
                        flag=flag,
                        result = true,
                        JwtToken
                    });
                }
                //登录失败，返回默认值 
                return JsonConvert.SerializeObject(new
                {
                    flag,
                    result,
                    JwtToken
                });

            }
        [HttpGet]
        [Route("errPlay")]
        public IActionResult ErrPlay()
        {
            int a = 1, b =0;
            var r = a / b;
            return Ok(new { stata = true, r });
        }
        }
    }

